package com.mouse.magent.proxy.handler;


import com.mouse.magent.common.handler.CommonHandler;
import com.mouse.magent.common.protocol.Command;
import com.mouse.magent.common.protocol.Header;
import com.mouse.magent.common.protocol.Message;
import io.netty.channel.ChannelHandlerContext;


public class ProxyLocalHandler extends CommonHandler {

    private ChannelHandlerContext proxyCtx;
    private String remoteChannelId;

    public ProxyLocalHandler(ChannelHandlerContext proxyCtx, String remoteChannelId) {
        this.proxyCtx = proxyCtx;
        this.remoteChannelId = remoteChannelId;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Message message = new Message();
        message.setType(Command.REMOTE_DATA);
        message.setData((byte[]) msg);
        message.setHeader(new Header(remoteChannelId));
        proxyCtx.writeAndFlush(message);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Message message = new Message();
        message.setType(Command.REMOTE_DISCONNECT);
        message.setHeader(new Header(remoteChannelId));
        proxyCtx.writeAndFlush(message);
    }
}
